import { Tween } from 'cc';
import { _decorator, Component, Node, Label, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('UIHint')
export class UIHint extends Component {
    @property(Node)
    private content: Node;

    @property(Label)
    private label: Label;

    private static _originPos: Vec3 = null;

    // 隐藏
    public onHide: (node: Node) => void = null;

    // 动画
    private _tweenShow: Tween<Node>;
    private _tweenTime: number = 3;

    protected onLoad(): void {
        if (UIHint._originPos == null) {
            UIHint._originPos = this.content.position.clone();
        }

        this._tweenShow = this.content.DOMoveY(this._tweenTime, 150, { easing: "circOut" })
            .call(() => {
                this.onHide(this.node);
            });
    }

    protected onEnable(): void {
        this.node.setSiblingIndex(this.node.parent.children.length);
        if (UIHint._originPos != null) {
            this.content.position = UIHint._originPos.clone();
        }

        this._tweenShow.start();
    }

    public show(text: string) {
        this.label.string = text;
    }
}